# -*- coding: cp936 -*-
import sys
from ctypes import *

ENUMCHILDPROC = WINFUNCTYPE(c_int, c_int, c_int)
##BOOL CALLBACK EnumChildProc(
##
##    HWND hwnd,	// handle to child window
##    LPARAM lParam 	// application-defined value
##   );

class RECT(Structure):
    _fields_ = [('left', c_long),
                ('top', c_long),
                ('right', c_long),
                ('bottom', c_long)]
    def __str__(self):
        return str((self.left, self.top, self.right, self.bottom))

class POINT(Structure):
    _fields_ = [('x', c_long),
                ('y', c_long)]
    def __str__(self):
        return str((self.x, self.y))

def ErrorIfZero(handle):
    if handle == 0:
        raise WinError
    else:
        return handle

def enumwindow(hwnd):
    global enumchildprc
    windll.user32.EnumChildWindows(hwnd, enumchildprc, c_int(-1))

def EnumChildProc(hwnd, lParam):
    global wndlist
    lhWnd = hwnd
    rect = RECT()
    pt = POINT()
    windll.user32.GetCursorPos(byref(pt))

    winclassname = create_string_buffer(255)
    windll.user32.GetClassNameA(c_int(lhWnd), winclassname,255)
    wintitlename = create_string_buffer(255)
    windll.user32.GetWindowTextA(lhWnd, wintitlename, 255)

    windll.user32.GetWindowRect(lhWnd, byref(rect))

    if abs(rect.left-pt.x) + abs(rect.top-pt.y)<50:
        if wndlist.has_key(winclassname.value):
            wndlist[winclassname.value].append((wintitlename.value, str(rect)))
        else:
            wndlist[winclassname.value] = [(wintitlename.value, str(rect))]

    if len(winclassname.value)==0 or len(wintitlename.value)==0:
        return True

##    # by class name
##    if winclassname.value=='TkTopLevel' and wintitlename.value.find('.py')!=-1:
##        windll.user32.MoveWindow(hwnd, pt.x, pt.y, rect.right-rect.left, rect.bottom-rect.top, True)
##        print '%s moved to under cursor. (%s)'%(wintitlename.value,winclassname.value)
##    if winclassname.value=='ConsoleWindowClass':
##        windll.user32.MoveWindow(hwnd, pt.x, pt.y, rect.right-rect.left, rect.bottom-rect.top, True)
##        print '%s moved to under cursor. (%s)'%(wintitlename.value,winclassname.value)
##    if winclassname.value=='Chat View':
##        windll.user32.MoveWindow(hwnd, pt.x, pt.y, rect.right-rect.left, rect.bottom-rect.top, True)
##        print '%s moved to under cursor. (%s)'%(wintitlename.value,winclassname.value)
##    if winclassname.value=='CabinetWClass':
##        windll.user32.MoveWindow(hwnd, pt.x, pt.y, rect.right-rect.left, rect.bottom-rect.top, True)
##        print '%s moved to under cursor. (%s)'%(wintitlename.value,winclassname.value)
##    if winclassname.value=='ExploreWClass':
##        windll.user32.MoveWindow(hwnd, pt.x, pt.y, rect.right-rect.left, rect.bottom-rect.top, True)
##        print '%s moved to under cursor. (%s)'%(wintitlename.value,winclassname.value)
##    if winclassname.value=='MozillaUIWindowClass':
##        windll.user32.MoveWindow(hwnd, pt.x, pt.y, rect.right-rect.left, rect.bottom-rect.top, True)
##        print '%s moved to under cursor. (%s)'%(wintitlename.value,winclassname.value)
##        
##    # by title name
##    if wintitlename.value.find('Draw on Frame')!=-1:
##        windll.user32.MoveWindow(hwnd, -2, -2, rect.right-rect.left, rect.bottom-rect.top, True)
##        print '%s moved to under cursor. (%s)'%(wintitlename.value,winclassname.value)
##    if wintitlename.value.find('log messages')!=-1:
##        windll.user32.MoveWindow(hwnd, pt.x, pt.y, rect.right-rect.left, rect.bottom-rect.top, True)
##        print '%s moved to under cursor. (%s)'%(wintitlename.value,winclassname.value)
##    if wintitlename.value.find('ŴŴ')!=-1:
##        windll.user32.MoveWindow(hwnd, pt.x, pt.y, rect.right-rect.left, rect.bottom-rect.top, True)
##        print '%s moved to under cursor. (%s)'%(wintitlename.value,winclassname.value)


    return True

wndlist = {}
enumchildprc = ENUMCHILDPROC(EnumChildProc) # keep a reference to this callback
enumwindow(windll.user32.GetDesktopWindow())

for kk in wndlist.keys():
    print 'classname: %s'%kk
    for titname,rect in  wndlist[kk]:
        print '%s[%s]'%(titname,str(rect))
    print '*'*8
    print
##    raw_input()
## TkTopLevel  'winpos.py - c:\\home\\desktop\\t\\misc\\winpos.py'
## ConsoleWindowClass  ''
## 'Chat View'  ''
